package org.esgi.transformer.impl;

import org.esgi.model.impl.FormInfo;
import org.esgi.model.interfaces.IFormInfo;
import org.esgi.transformer.interfaces.IXMLFormInfoTransformer;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XMLFormInfoTransformer implements IXMLFormInfoTransformer {

	@Override
	public IFormInfo formInfoFromElement(Element e) {
		NodeList nl = e.getChildNodes();
		IFormInfo formInfo = new FormInfo();
		for(int i = 0; i < nl.getLength(); i++){
			if(nl.item(i).getNodeName().equals("id")){
				Node textNode = nl.item(i).getFirstChild();
				if(textNode != null)
					formInfo.setId(textNode.getNodeValue());
			}else if(nl.item(i).getNodeName().equals("name")){
				Node textNode = nl.item(i).getFirstChild();
				if(textNode != null)
					formInfo.setName(textNode.getNodeValue());
			}else if(nl.item(i).getNodeName().equals("questionCount")){
				Node textNode = nl.item(i).getFirstChild();
				if(textNode != null)
					formInfo.setQuestionsCount(Integer.parseInt(textNode.getNodeValue()));
			}
		}
		return formInfo;
	}

}
